/**
 * 
 */
package com.easidea.tim.service.customer;

import java.util.Date;
import java.util.List;

import org.apache.log4j.Logger;
import org.hibernate.Criteria;
import org.hibernate.criterion.Restrictions;
import org.springside.core.service.EntityService;

import com.easidea.tim.dao.customer.CustomerAccountMemoDao;
import com.easidea.tim.entity.customer.CustomerAccountMemo;

/**
 * @author Harrison
 *
 */
public class CustomerAccountMemoService extends EntityService<CustomerAccountMemo,CustomerAccountMemoDao> {

	private CustomerAccountMemoDao customerAccountMemoDao;
	private static final Logger log = Logger.getLogger(CustomerAccountMemoService.class);

	/**
	 * 保存，若存在以前的记录，直接提取后修改，否则创建新纪录
	 * @param memo
	 */
	public void saveMemo(CustomerAccountMemo caMemo){
		if(caMemo == null){
			log.warn("object is null , could not save the customer account memo data . ");
		}else if (caMemo.getCustomer() == null || caMemo.getMemo() == null) {
			log.warn("customer is null or memo data is null in save the customer account memo data ...");
		}else {
			Criteria cte = createUnDeletedCriteria();
			cte.add(Restrictions.eq("customer.id", caMemo.getCustomer().getId()));
			cte.setFetchSize(1);
			List<CustomerAccountMemo> resultList = (List<CustomerAccountMemo>)cte.list();
			
			if(resultList != null && resultList.size() == 1){
				CustomerAccountMemo camm = resultList.get(0);
				if(camm != null){
					camm.setMemo(caMemo.getMemo());
					camm.setModifier(caMemo.getModifier());
					camm.setModifyTime(new Date());
					customerAccountMemoDao.merge(camm);
				}
			}else{
				log.info("CustomerAccountMemo dose not exist with customer id: " + caMemo.getCustomer().getId() + ", system will create it then ....");
				customerAccountMemoDao.save(caMemo);
			}
		}
	}
	
	
	
	/**
	 * @return the customerAccountMemoDao
	 */
	public CustomerAccountMemoDao getCustomerAccountMemoDao() {
		return customerAccountMemoDao;
	}

	/**
	 * @param customerAccountMemoDao the customerAccountMemoDao to set
	 */
	public void setCustomerAccountMemoDao(
			CustomerAccountMemoDao customerAccountMemoDao) {
		this.customerAccountMemoDao = customerAccountMemoDao;
	}

	/* (non-Javadoc)
	 * @see org.springside.core.service.EntityService#getEntityDao()
	 */
	@Override
	public CustomerAccountMemoDao getEntityDao() {
		return customerAccountMemoDao;
	}

}
